<?php
/**
 * Created by PhpStorm.
 * User: zeopean
 * Date: 2015/10/29
 * Time: 16:09
 */
namespace App\Services;


use App\Exceptions\TagsException;
use App\Tags;
use Illuminate\Http\Request;

class TagsManager{

    public function EditTags(Request $request ){
        if(!is_null($request->get('tag')))
            $tagStr    = $request->get('tag');
        else
            throw new TagsException(TagsException::TagNull);

        if(!is_null($request->get('project_id')))  $projectId   = $request->get('project_id');
        if(!is_null($request->get('investor_id'))) $investorId  = $request->get('investor_id');
        if(!is_null($request->get('org_id')))      $orgId       = $request->get('org_id');

        /** 对tags 参数进行判断 是否存在 */
        $tags = $this->TagsIsExists($tagStr) ;          //return  $tags['id'=> , 'new'=>]

        if(!$tags) return false;

        if(!empty($tags['ids'])){
            /** 判断标签关联是否存在 */
            $Tags = Tags::whereId($tags['ids'])->get();
            foreach($tags['ids'] as $tagId){
                if(isset($projectId))  $this->ProjectTagsIsExists($projectId , $tagId) ;
                if(isset($investorId)) $this->InvestorTagsIsExists($investorId , $tagId) ;
                if(isset($orgId))      $this->InvestOrgTagsIsExists($orgId , $tagId) ;
            }
        }

        if(!empty($tags['new'])){

            foreach($tags['new'] as $tag){
                $Tags = new Tags();
                $Tags->tag  = $tag ;
                $Tags -> save();
                $tagId = $Tags->id;
                if(isset($projectId))  $this->ProjectTagsIsExists($projectId , $tagId) ;
                if(isset($investorId)) $this->InvestorTagsIsExists($investorId , $tagId) ;
                if(isset($orgId))      $this->InvestOrgTagsIsExists($orgId , $tagId) ;
            }
        }

        return $Tags;
    }

    /**
     * 判断标签 是否已经存在
     * @param $tagStr
     * @return array
     */
    protected function TagsIsExists($tagStr){
        /** 对tag 标签进行处理 */
        if($tagStr == '') return false;
        if(strpos($tagStr , '，')){
            $tagStr = str_replace('，' ,',' , $tagStr);
            $tagStr = trim($tagStr , ',');
        }
        $tmp        = explode(',' , $tagStr);
        $tags       = [] ;
        foreach($tmp as $tag){
            if($tagIds =  Tags::whereTag($tag)->pluck('id'))
                $tags['ids'][] = $tagIds ;      //获取tag标志id
            if(!$tagIds)
                $tags['new'][] = $tag;          //表示不存在的标签
        }

        if($tags) return $tags;
    }

    /**
     * 判断项目标签是否在关系表中存在
     * @param $projectId
     * @param $tagId
     */
    protected function ProjectTagsIsExists($projectId , $tagId)
    {
        $Tags   = Tags::find($tagId);
        if($Tags->project_x_tags)
            if(!$Tags->project_x_tags()->where('project_id' ,'=' , $projectId)->exists())
                $Tags->project_x_tags()->attach($projectId);
    }

    /**
     * 判断投资人标签是否在关系表中存在
     * @param $investorId
     * @param $tagId
     */
    protected function InvestorTagsIsExists($investorId , $tagId)
    {
        $Tags   = Tags::find($tagId);
        if($Tags->investor_x_tags)
            if(!$Tags->investor_x_tags()->where('investor_id' ,'=' , $investorId)->exists())
                $Tags->investor_x_tags()->attach($investorId);
    }

    /**
     * 判断投资机构是否在标签中存在
     * @param $orgId
     * @param $tagId
     */
    protected function InvestOrgTagsIsExists($orgId , $tagId)
    {
        $Tags   = Tags::find($tagId);
        if($Tags->investorg_x_tags)
            if(!$Tags->investorg_x_tags()->where('org_id' ,'=' , $orgId)->exists())
                $Tags->investorg_x_tags()->attach($orgId);

    }



    public function deleteTags($id=0){
        /** 判断用户是否登录*/

        return true;
    }
}